package com.scs.application.core.process.aspect;


import com.scs.application.core.exceptions.BusinessException;
import com.scs.application.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description:
 */
@Slf4j
@Aspect
@Component
@Order(-1)
public class ProcessAspect {

    private final String POINT_CUT = "execution(public * fortec..*.service.*.*(..))";

    @Pointcut(POINT_CUT)
    public void pointCut(){}

    @Around("pointCut()")
    public Object process(ProceedingJoinPoint joinPoint) throws Throwable {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String $processKey = request.getParameter("$processKey");

        boolean inFlow = StringUtils.isNotBlank($processKey);

        if(!inFlow){
            return joinPoint.proceed();
        }

        if(!TransactionSynchronizationManager.isSynchronizationActive()){
            throw new BusinessException("当前流程未处于事务中，请联系系统管理人员");
        }

        // 获取流程信息

        // 获取流程处理对象


        return joinPoint.proceed();
    }
}
